import request from '@/sheep/request';

// 轮胎取单 VO
export interface ReleaseRecordsVO {
  storeId: number; // 门店ID
  depositRecordsId: number; // 轮胎存单ID
  licensePlate: string; // 车牌号
  contactNumber: string; // 联系人手机号
  storageType: string; // 存储状态
  status: number; // 取胎状态，0 草稿，1 已预约，2 已接单，3 已接收，4 已出库
  notes: string; // 备注信息
  id: number; // 取胎记录ID，主键
  createTime: Date; // 创建时间
}

// 轮胎取单 API
export const ReleaseRecordsApi = {
  // 查询轮胎取单分页
  getReleaseRecordsPage: async (data: any) => {
    let url = '/tire/release-records/page?';
    if (data) {
      for (let key in data) {
        const value = data[key];
        if (Array.isArray(value)) {
          value.forEach(val => {
            url += `${key}=${val}&`;
          });
        } else {
          url += `${key}=${value}&`;
        }
      }
    }
    // 去掉最后一个多余的 "&"
    url = url.slice(0, -1);
    return await request({ url, method: 'GET' });
  },
  // 查询轮胎取单详情
  getReleaseRecords: async (id: number) => {
    return await request({ url: `/tire/release-records/get?id=` + id, method: 'GET' });
  },

  // 新增轮胎取单
  createReleaseRecords: async (data: ReleaseRecordsVO) => {
    return await request({ url: `/tire/release-records/create`, method: 'POST', data });
  },

  // 修改轮胎取单
  updateReleaseRecords: async (data: ReleaseRecordsVO) => {
    return await request({ url: `/tire/release-records/update`, method: 'PUT', data });
  },

  // 删除轮胎取单
  deleteReleaseRecords: async (id: number) => {
    return await request({ url: `/tire/release-records/delete?id=` + id, method: 'DELETE' });
  },

  // ====== 子表（轮胎取单明细） ======

  // 获得轮胎取单明细列表
  getReleaseRecordsDetailListByTireReleaseRecordsId: async (tireReleaseRecordsId) => {
    return await request({
      url:
        `/tire/release-records/release-records-detail/list-by-tire-release-records-id?tireReleaseRecordsId=` +
        tireReleaseRecordsId,
      method: 'GET',
    });
  },

  // 根据存单ID生成取单
  createWithdrawalFromDeposit: async (data: ReleaseRecordsVO) => {
    return await request({
      url: `/tire/release-records/createWithdrawalFromDeposit`,
      method: 'POST',
      data,
    });
  },
};
